<?php
namespace app\admin\model;
use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;

class Goods extends Model
{
    protected $pk = 'goods_id';

    //当轮播图字段不为空时,转成数组
    public function getBannerImageAttr($v)
    {
        if (!empty($v)){
            return explode(',',$v);
        }
    }

    public function searchKeywordsAttr($q,$v,$d)
    {
        $q->where('goods_name','like','%'.$v.'%');
    }
    public function searchRecommendedAttr($q,$v,$d)
    {
        $q->where('recommended','=',$v);
    }
    public function searchIfShowAttr($q,$v,$d)
    {
        $q->where('if_show','=',$v);
    }
    public function searchBrandIdAttr($q,$v,$d)
    {
        $q->where('brand','=',$v);
    }
    public function searchStartTimeAttr($q,$v,$d)
    {
        $q->whereTime('add_time','>',$v);
    }
    public function searchEndTimeAttr($q,$v,$d)
    {
        $q->whereTime('add_time','<',$v);
    }

    public static function getGoodsList($data,$order,$param)
    {
        $list =  Goods::name('goods')
            ->withSearch(array_keys($data),$data)
            ->order($order)
            ->paginate([
                'list_rows'=>5,
                'query'=> $data
            ]);

        if (isset($param['page']) && $param['page']>$list->currentPage()){
            $list =  Goods::name('goods')
                ->withSearch(array_keys($data),$data)
                ->order($order)
                ->paginate([
                    'page'=>$list->currentPage(),
                    'list_rows'=>1,
                    'query'=> $data
                ]);
        }
        return $list;
    }

    public static function addData($data)
    {
        return self::create($data);
    }
    public static function getGoodsPropList()
    {
        return Db::name('goods_prop')
            ->field(true)
            //->field("if((status=1),'有效','无效') status")
            ->order('sort_order')
            ->select();

    }


    //商品添加
    public static function   AddGoodsInfo($data){
        try{
            $res=Goods::strict(true)->insert($data);
        }catch (Exception $e){
            $res=false;
        }
        return $res;
    }
    //商品修改
    public static function   goodEditInfo($post,$get){
        if (Request::isPost()){
            try{
                $res=Goods::update($post);
            }catch (Exception $e){
                $res=false;
            }
            return $res;
        }else{
        }
        $find=Goods::where('goods_id','=',$get['goods_id'])->find();
        return $find;
    }

    //查询品牌下拉处理
    public static function   goodsPin(){
        $pin=Brand::field('brand_id,brand_name')->select();
        return $pin;
    }
    //查询分类下拉处理
    public static function  gcate(){
        $cate=Gcategory::field("cate_id,cate_name,parent_id")->select()->toArray();
        return $cate;
    }









}
